rm(list=ls())
library(tidyverse)

# Create folder to receive data
dir.create(here::here("data","raw","bolsa_familia"))

# Date of download
date <- Sys.Date()

#Download all files from http://www.dados.gov.br/dataset/bolsa-familia-misocial

years <- 2004:2020

list_bolsa_fam <- map(years, ~paste0("http://aplicacoes.mds.gov.br/sagi/servicos/misocial?q=*&fq=anomes_s:",.x,"*&fq=tipo_s:mes_mu&wt=csv&fl=ibge:codigo_ibge,anomes:anomes_s,qtd_familias_beneficiarias_bolsa_familia,valor_repassado_bolsa_familia&rows=10000000&sort=anomes_s%20asc,%20codigo_ibge%20asc"))

list_save_rds <- map(years, ~here::here(paste0("data/raw/bolsa_familia/bolsa_fam_",.x,"_downloaded_",date,".csv")))

walk2(list_bolsa_fam,list_save_rds,~download.file(url = .x,destfile = .y))
